package models

import (
	orm "go-papers/database"
	_ "go-papers/tools"
	_ "time"
)

type FeedBack struct {
	Id                int    `json:"id" gorm:"primary_key;auto_increment"`        // 自增主键
	MemberId          int    `json:"memberId" gorm:"type:int(11);"`               // 反馈人员 id
	MemberName        string `json:"memberName" gorm:"type:varchar(255);"`        // 昵称
	OpenId            string `json:"openId" gorm:"type:varchar(255);"`            // 昵称
	ThirdUserId       int    `json:"thirdUserId" gorm:"type:int(11);"`            // 昵称
	MemberPhone       string `json:"memberPhone" gorm:"type:varchar(255);"`       // 联系电话
	MemberRegisterDay string `json:"memberRegisterDay" gorm:"type:varchar(128);"` // 联系电话
	MemberAddr        string `json:"memberAddr" gorm:"type:varchar(255);"`        // 联系地址
	MemberMomId       int    `json:"memberMomId" gorm:"type:int(11);"`            //管家妈妈id
	MemberMomName     string `json:"memberMomName" gorm:"type:varchar(255);"`     // 管家妈妈名称
	UserId            int    `json:"userId" gorm:"type:int(11);"`                 // 处理人id
	FeedType          string `json:"feedType" gorm:"type:tinyint(1);"`            // 反馈类型 1 投诉，2 建议
	FeedDesc          string `json:"feedDesc" gorm:"type:text;"`                  // 反馈明细
	Img               string `json:"img" gorm:"type:text;"`                       // 反馈图片地址

	FeedTime   string `json:"feedTime" gorm:"type:varchar(32);"`  // 反馈时间
	FeedStatus string `json:"feedStatus" gorm:"type:tinyint(1);"` // 0 删除 1 待处理  5 处理中  10 已完结
	FeedBack   string `json:"feedBack" gorm:"type:varchar(255);"` // 问题小结
	FeedStart  string `json:"-" gorm:"-"`                         //
	FeedEnd    string `json:"-" gorm:"-"`                         //
	//Img3       string `json:"img3" gorm:"type:varchar(255);"`     //
	DataScope string `json:"dataScope" gorm:"-"`
	Params    string `json:"params"  gorm:"-"`
	BaseModel
}

func (FeedBack) TableName() string {
	return "feed_back"
}

// 创建FeedBack
func (e *FeedBack) Create() (FeedBack, error) {
	var doc FeedBack
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

func (e *FeedBack) GetByGw() (FeedBack, error) {
	var doc FeedBack
	table := orm.Eloquent.Table(e.TableName())
	if e.Id != 0 {
		table = table.Where("id = ?", e.Id)
	}
	if e.MemberId != 0 {
		table = table.Where("member_id = ?", e.MemberId)
	} else {
		if e.OpenId != "" {
			table = table.Where("third_user_id = ?", e.ThirdUserId)
			table = table.Where("open_id = ?", e.OpenId)
		} else {
			table = table.Where("id = ?", 0)
		}
	}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取FeedBack
func (e *FeedBack) Get() (FeedBack, error) {
	var doc FeedBack
	table := orm.Eloquent.Table(e.TableName())
	if e.Id != 0 {
		table = table.Where("id = ?", e.Id)
	}
	if e.MemberId != 0 {
		table = table.Where("member_id = ?", e.MemberId)
	}
	if e.OpenId != "" {
		table = table.Where("open_id = ?", e.OpenId)
	}
	if e.MemberName != "" {
		table = table.Where("member_name like ?", "%"+e.MemberName+"%")
	}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// GetPageSearch
func (e *FeedBack) GetPageSearch(pageSize int, pageIndex int) ([]FeedBack, int, error) {
	var doc []FeedBack

	table := orm.Eloquent.Select("*").Table(e.TableName())
	if e.Id != 0 {
		table = table.Where("id = ?", e.Id)
	}
	if e.MemberId != 0 {
		table = table.Where("member_id = ?", e.MemberId)
	} else {
		table = table.Where("third_user_id = ?", e.ThirdUserId)
		table = table.Where("open_id = ?", e.OpenId)
	}
	if e.MemberName != "" {
		table = table.Where("member_name like ?", e.MemberName+"%")
	}

	if e.FeedStatus != "" {
		table = table.Where("feed_status = ?", e.FeedStatus)
	}

	if e.FeedStart != "" {
		table = table.Where("feed_time >=  ?", e.FeedStart)
	}
	if e.FeedType != "" {
		table = table.Where("feed_type =  ?", e.FeedType)
	}
	if e.FeedEnd != "" {
		table = table.Where("feed_time <=  ?", e.FeedEnd)
	}
	if e.MemberAddr != "" {
		table = table.Where("member_addr like  ?", "%"+e.MemberAddr+"%")
	}

	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where(" `feed_status` != 0 ").Count(&count)
	return doc, count, nil
}

// 获取FeedBack带分页
func (e *FeedBack) GetPage(pageSize int, pageIndex int) ([]FeedBack, int, error) {
	var doc []FeedBack

	table := orm.Eloquent.Select("*").Table(e.TableName())
	if e.Id != 0 {
		table = table.Where("id = ?", e.Id)
	}
	if e.MemberId != 0 {
		table = table.Where("member_id = ?", e.MemberId)
	}
	if e.MemberName != "" {
		table = table.Where("member_name like ?", e.MemberName+"%")
	}

	if e.FeedStatus != "" {
		table = table.Where("feed_status = ?", e.FeedStatus)
	}

	if e.FeedStart != "" {
		table = table.Where("feed_time >=  ?", e.FeedStart)
	}
	if e.FeedType != "" {
		table = table.Where("feed_type =  ?", e.FeedType)
	}
	if e.FeedEnd != "" {
		table = table.Where("feed_time <=  ?", e.FeedEnd)
	}
	if e.MemberAddr != "" {
		table = table.Where("member_addr like  ?", "%"+e.MemberAddr+"%")
	}

	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where(" `feed_status` != 0 ").Count(&count)
	return doc, count, nil
}

// 更新FeedBack
func (e *FeedBack) Update(id int) (update FeedBack, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

// 删除FeedBack
func (e *FeedBack) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", id).Delete(&FeedBack{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *FeedBack) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id in (?)", id).Delete(&FeedBack{}).Error; err != nil {
		return
	}
	Result = true
	return
}
